package org.dromara.yuanshidian.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.yuanshidian.domain.bo.Ab01Bo;
import org.dromara.yuanshidian.domain.vo.Ab01Vo;
import org.dromara.yuanshidian.domain.Ab01;
import org.dromara.yuanshidian.mapper.Ab01Mapper;
import org.dromara.yuanshidian.service.IAb01Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 原单位信息Service业务层处理
 *
 * @author tzhongtop
 * @date 2025-01-24
 */
@RequiredArgsConstructor
@Service
public class Ab01ServiceImpl implements IAb01Service {

    private final Ab01Mapper baseMapper;

    /**
     * 查询原单位信息
     *
     * @param id 主键
     * @return 原单位信息
     */
    @Override
    public Ab01Vo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询原单位信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 原单位信息分页列表
     */
    @Override
    public TableDataInfo<Ab01Vo> queryPageList(Ab01Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Ab01> lqw = buildQueryWrapper(bo);
        Page<Ab01Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的原单位信息列表
     *
     * @param bo 查询条件
     * @return 原单位信息列表
     */
    @Override
    public List<Ab01Vo> queryList(Ab01Bo bo) {
        LambdaQueryWrapper<Ab01> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Ab01> buildQueryWrapper(Ab01Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Ab01> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAaa027()), Ab01::getAaa027, bo.getAaa027());
        lqw.eq(StringUtils.isNotBlank(bo.getDwbm()), Ab01::getDwbm, bo.getDwbm());
        lqw.eq(StringUtils.isNotBlank(bo.getBh()), Ab01::getBh, bo.getBh());
        lqw.eq(StringUtils.isNotBlank(bo.getZh1()), Ab01::getZh1, bo.getZh1());
        lqw.eq(StringUtils.isNotBlank(bo.getZh2()), Ab01::getZh2, bo.getZh2());
        lqw.eq(StringUtils.isNotBlank(bo.getDwkfh()), Ab01::getDwkfh, bo.getDwkfh());
        lqw.eq(StringUtils.isNotBlank(bo.getDw01()), Ab01::getDw01, bo.getDw01());
        lqw.like(StringUtils.isNotBlank(bo.getDw02()), Ab01::getDw02, bo.getDw02());
        lqw.eq(StringUtils.isNotBlank(bo.getDw03()), Ab01::getDw03, bo.getDw03());
        lqw.eq(StringUtils.isNotBlank(bo.getDw04()), Ab01::getDw04, bo.getDw04());
        lqw.eq(StringUtils.isNotBlank(bo.getDw05()), Ab01::getDw05, bo.getDw05());
        lqw.eq(StringUtils.isNotBlank(bo.getDw06()), Ab01::getDw06, bo.getDw06());
        lqw.eq(StringUtils.isNotBlank(bo.getDw07()), Ab01::getDw07, bo.getDw07());
        lqw.eq(StringUtils.isNotBlank(bo.getDw08()), Ab01::getDw08, bo.getDw08());
        lqw.eq(StringUtils.isNotBlank(bo.getDw09()), Ab01::getDw09, bo.getDw09());
        lqw.eq(StringUtils.isNotBlank(bo.getDw10()), Ab01::getDw10, bo.getDw10());
        lqw.eq(StringUtils.isNotBlank(bo.getDw12()), Ab01::getDw12, bo.getDw12());
        lqw.eq(StringUtils.isNotBlank(bo.getDw13()), Ab01::getDw13, bo.getDw13());
        lqw.eq(bo.getDw14() != null, Ab01::getDw14, bo.getDw14());
        lqw.eq(StringUtils.isNotBlank(bo.getDw17()), Ab01::getDw17, bo.getDw17());
        lqw.eq(StringUtils.isNotBlank(bo.getDw19()), Ab01::getDw19, bo.getDw19());
        lqw.eq(bo.getDw21() != null, Ab01::getDw21, bo.getDw21());
        lqw.eq(bo.getDw22() != null, Ab01::getDw22, bo.getDw22());
        lqw.eq(bo.getDw23() != null, Ab01::getDw23, bo.getDw23());
        lqw.eq(StringUtils.isNotBlank(bo.getDw24()), Ab01::getDw24, bo.getDw24());
        lqw.eq(StringUtils.isNotBlank(bo.getDw25()), Ab01::getDw25, bo.getDw25());
        lqw.eq(StringUtils.isNotBlank(bo.getDw27()), Ab01::getDw27, bo.getDw27());
        lqw.eq(StringUtils.isNotBlank(bo.getDw28()), Ab01::getDw28, bo.getDw28());
        lqw.eq(bo.getDw29() != null, Ab01::getDw29, bo.getDw29());
        lqw.eq(bo.getDw30() != null, Ab01::getDw30, bo.getDw30());
        lqw.eq(bo.getDw31() != null, Ab01::getDw31, bo.getDw31());
        lqw.eq(bo.getDw32() != null, Ab01::getDw32, bo.getDw32());
        lqw.eq(bo.getDw33() != null, Ab01::getDw33, bo.getDw33());
        lqw.eq(StringUtils.isNotBlank(bo.getDw34()), Ab01::getDw34, bo.getDw34());
        lqw.eq(StringUtils.isNotBlank(bo.getDw35()), Ab01::getDw35, bo.getDw35());
        lqw.eq(StringUtils.isNotBlank(bo.getDw36()), Ab01::getDw36, bo.getDw36());
        lqw.eq(bo.getDw37() != null, Ab01::getDw37, bo.getDw37());
        lqw.eq(StringUtils.isNotBlank(bo.getDw38()), Ab01::getDw38, bo.getDw38());
        lqw.eq(StringUtils.isNotBlank(bo.getDw41()), Ab01::getDw41, bo.getDw41());
        lqw.eq(StringUtils.isNotBlank(bo.getDw42()), Ab01::getDw42, bo.getDw42());
        lqw.eq(StringUtils.isNotBlank(bo.getDw43()), Ab01::getDw43, bo.getDw43());
        lqw.eq(StringUtils.isNotBlank(bo.getDw44()), Ab01::getDw44, bo.getDw44());
        lqw.eq(StringUtils.isNotBlank(bo.getDw60()), Ab01::getDw60, bo.getDw60());
        lqw.eq(StringUtils.isNotBlank(bo.getDw61()), Ab01::getDw61, bo.getDw61());
        lqw.eq(StringUtils.isNotBlank(bo.getDw62()), Ab01::getDw62, bo.getDw62());
        lqw.eq(bo.getDw63() != null, Ab01::getDw63, bo.getDw63());
        lqw.eq(bo.getDw64() != null, Ab01::getDw64, bo.getDw64());
        lqw.eq(bo.getDw65() != null, Ab01::getDw65, bo.getDw65());
        lqw.eq(StringUtils.isNotBlank(bo.getDw66()), Ab01::getDw66, bo.getDw66());
        lqw.eq(StringUtils.isNotBlank(bo.getDw67()), Ab01::getDw67, bo.getDw67());
        lqw.eq(StringUtils.isNotBlank(bo.getDw68()), Ab01::getDw68, bo.getDw68());
        lqw.eq(StringUtils.isNotBlank(bo.getDw69()), Ab01::getDw69, bo.getDw69());
        lqw.eq(bo.getDw70() != null, Ab01::getDw70, bo.getDw70());
        lqw.eq(StringUtils.isNotBlank(bo.getDw71()), Ab01::getDw71, bo.getDw71());
        lqw.eq(StringUtils.isNotBlank(bo.getDw72()), Ab01::getDw72, bo.getDw72());
        lqw.eq(bo.getDw73() != null, Ab01::getDw73, bo.getDw73());
        lqw.eq(bo.getDw74() != null, Ab01::getDw74, bo.getDw74());
        lqw.eq(bo.getDw75() != null, Ab01::getDw75, bo.getDw75());
        lqw.eq(StringUtils.isNotBlank(bo.getDw76()), Ab01::getDw76, bo.getDw76());
        lqw.eq(StringUtils.isNotBlank(bo.getDw77()), Ab01::getDw77, bo.getDw77());
        lqw.eq(StringUtils.isNotBlank(bo.getDw78()), Ab01::getDw78, bo.getDw78());
        lqw.eq(StringUtils.isNotBlank(bo.getDw79()), Ab01::getDw79, bo.getDw79());
        lqw.eq(StringUtils.isNotBlank(bo.getTz01()), Ab01::getTz01, bo.getTz01());
        lqw.eq(bo.getTz04() != null, Ab01::getTz04, bo.getTz04());
        lqw.eq(bo.getTz05() != null, Ab01::getTz05, bo.getTz05());
        lqw.eq(bo.getTz07() != null, Ab01::getTz07, bo.getTz07());
        lqw.eq(bo.getTz08() != null, Ab01::getTz08, bo.getTz08());
        lqw.eq(bo.getTz14() != null, Ab01::getTz14, bo.getTz14());
        lqw.eq(bo.getTz25() != null, Ab01::getTz25, bo.getTz25());
        lqw.eq(bo.getTz26() != null, Ab01::getTz26, bo.getTz26());
        lqw.eq(bo.getTz28() != null, Ab01::getTz28, bo.getTz28());
        lqw.eq(bo.getTz64() != null, Ab01::getTz64, bo.getTz64());
        lqw.eq(bo.getTzce() != null, Ab01::getTzce, bo.getTzce());
        lqw.eq(StringUtils.isNotBlank(bo.getDh1()), Ab01::getDh1, bo.getDh1());
        lqw.eq(StringUtils.isNotBlank(bo.getDh2()), Ab01::getDh2, bo.getDh2());
        lqw.eq(StringUtils.isNotBlank(bo.getDh3()), Ab01::getDh3, bo.getDh3());
        lqw.eq(bo.getSystime() != null, Ab01::getSystime, bo.getSystime());
        lqw.eq(bo.getSjjsq() != null, Ab01::getSjjsq, bo.getSjjsq());
        lqw.eq(bo.getBfjsq() != null, Ab01::getBfjsq, bo.getBfjsq());
        lqw.eq(bo.getKys() != null, Ab01::getKys, bo.getKys());
        lqw.eq(bo.getKje() != null, Ab01::getKje, bo.getKje());
        lqw.eq(StringUtils.isNotBlank(bo.getPass()), Ab01::getPass, bo.getPass());
        lqw.eq(StringUtils.isNotBlank(bo.getDw83()), Ab01::getDw83, bo.getDw83());
        lqw.eq(StringUtils.isNotBlank(bo.getPch()), Ab01::getPch, bo.getPch());
        lqw.eq(bo.getDw81() != null, Ab01::getDw81, bo.getDw81());
        lqw.eq(bo.getDw84() != null, Ab01::getDw84, bo.getDw84());
        lqw.eq(StringUtils.isNotBlank(bo.getZnksyf()), Ab01::getZnksyf, bo.getZnksyf());
        lqw.eq(bo.getQqljqj() != null, Ab01::getQqljqj, bo.getQqljqj());
        lqw.eq(bo.getZnjfpbl() != null, Ab01::getZnjfpbl, bo.getZnjfpbl());
        lqw.eq(StringUtils.isNotBlank(bo.getHkjhbz()), Ab01::getHkjhbz, bo.getHkjhbz());
        lqw.eq(bo.getHkjedz() != null, Ab01::getHkjedz, bo.getHkjedz());
        lqw.eq(StringUtils.isNotBlank(bo.getDw80()), Ab01::getDw80, bo.getDw80());
        lqw.eq(bo.getDw85() != null, Ab01::getDw85, bo.getDw85());
        lqw.eq(StringUtils.isNotBlank(bo.getDw86()), Ab01::getDw86, bo.getDw86());
        lqw.eq(StringUtils.isNotBlank(bo.getDw87()), Ab01::getDw87, bo.getDw87());
        lqw.eq(StringUtils.isNotBlank(bo.getDw88()), Ab01::getDw88, bo.getDw88());
        lqw.eq(StringUtils.isNotBlank(bo.getDw51()), Ab01::getDw51, bo.getDw51());
        lqw.eq(StringUtils.isNotBlank(bo.getDw52()), Ab01::getDw52, bo.getDw52());
        lqw.eq(bo.getDw29a() != null, Ab01::getDw29a, bo.getDw29a());
        lqw.eq(bo.getDw29b() != null, Ab01::getDw29b, bo.getDw29b());
        lqw.eq(bo.getDw31a() != null, Ab01::getDw31a, bo.getDw31a());
        lqw.eq(bo.getDw31b() != null, Ab01::getDw31b, bo.getDw31b());
        lqw.eq(StringUtils.isNotBlank(bo.getDwcbzt()), Ab01::getDwcbzt, bo.getDwcbzt());
        lqw.eq(StringUtils.isNotBlank(bo.getDwyhhm()), Ab01::getDwyhhm, bo.getDwyhhm());
        lqw.eq(StringUtils.isNotBlank(bo.getSfbj()), Ab01::getSfbj, bo.getSfbj());
        lqw.eq(StringUtils.isNotBlank(bo.getSybz01()), Ab01::getSybz01, bo.getSybz01());
        lqw.eq(StringUtils.isNotBlank(bo.getSybz02()), Ab01::getSybz02, bo.getSybz02());
        lqw.eq(StringUtils.isNotBlank(bo.getSybz03()), Ab01::getSybz03, bo.getSybz03());
        lqw.eq(StringUtils.isNotBlank(bo.getSytz01()), Ab01::getSytz01, bo.getSytz01());
        lqw.eq(StringUtils.isNotBlank(bo.getSytz02()), Ab01::getSytz02, bo.getSytz02());
        lqw.eq(bo.getTz117() != null, Ab01::getTz117, bo.getTz117());
        lqw.eq(StringUtils.isNotBlank(bo.getZcfl()), Ab01::getZcfl, bo.getZcfl());
        lqw.eq(StringUtils.isNotBlank(bo.getDw91()), Ab01::getDw91, bo.getDw91());
        lqw.eq(bo.getDw90() != null, Ab01::getDw90, bo.getDw90());
        lqw.eq(StringUtils.isNotBlank(bo.getDwjd()), Ab01::getDwjd, bo.getDwjd());
        lqw.eq(StringUtils.isNotBlank(bo.getDwjp()), Ab01::getDwjp, bo.getDwjp());
        lqw.eq(StringUtils.isNotBlank(bo.getDwsjfs()), Ab01::getDwsjfs, bo.getDwsjfs());
        lqw.eq(StringUtils.isNotBlank(bo.getDwsq()), Ab01::getDwsq, bo.getDwsq());
        lqw.eq(StringUtils.isNotBlank(bo.getDwxzq()), Ab01::getDwxzq, bo.getDwxzq());
        lqw.eq(StringUtils.isNotBlank(bo.getWtshm()), Ab01::getWtshm, bo.getWtshm());
        lqw.eq(StringUtils.isNotBlank(bo.getXndwbz()), Ab01::getXndwbz, bo.getXndwbz());
        lqw.eq(bo.getZnjjsrq() != null, Ab01::getZnjjsrq, bo.getZnjjsrq());
        lqw.eq(StringUtils.isNotBlank(bo.getZzjgdm()), Ab01::getZzjgdm, bo.getZzjgdm());
        lqw.eq(bo.getStatus() != null, Ab01::getStatus, bo.getStatus());
        lqw.eq(bo.getDelStatus() != null, Ab01::getDelStatus, bo.getDelStatus());
        lqw.eq(bo.getRefStatus() != null, Ab01::getRefStatus, bo.getRefStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFileurl()), Ab01::getFileurl, bo.getFileurl());
        return lqw;
    }

    /**
     * 新增原单位信息
     *
     * @param bo 原单位信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Ab01Bo bo) {
        Ab01 add = MapstructUtils.convert(bo, Ab01.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改原单位信息
     *
     * @param bo 原单位信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Ab01Bo bo) {
        Ab01 update = MapstructUtils.convert(bo, Ab01.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Ab01 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除原单位信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 导出查询单位信息列表
     * @param ab01 单位信息
     * @return 单位信息集合
     */
    @Override
    public List<Ab01Vo> exportAb01(Ab01Bo ab01, String[] splitID) {

        return baseMapper.exportAb01(ab01,splitID);
    }
}
